package com.fzk.blog.settings.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fzk.blog.bean.Role;
import com.fzk.blog.bean.vo.RoleInfo;
import com.fzk.blog.blog.service.IRedisService;
import com.fzk.blog.exception.MyException;
import com.fzk.blog.mapper.RoleMapper;
import com.fzk.blog.settings.service.IRoleService;
import com.fzk.blog.utils.MyJsonUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author fzk
 * @date 2021-08-18 0:52
 */
@Service
public class RoleService implements IRoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private IRedisService redisService;

    @Override
    public RoleInfo getRoleInfoById(int id) throws JsonProcessingException {
        RoleInfo roleInfo = null;
        // 1.从redis拿
        String json = redisService.get("roleInfo:id:" + id);

        // 2.没有则从数据库查，并放入redis
        if (json != null)
            roleInfo = MyJsonUtil.toBean(json, RoleInfo.class);
        else {
            roleInfo = roleMapper.getRoleInfoById(id);
            if (roleInfo == null)
                throw new MyException(400, "参数错误：id不存在：" + id);
            redisService.setEx("roleInfo:id:" + id, MyJsonUtil.writeToString(roleInfo));
        }

        return roleInfo;
    }

    @Override
    public List<RoleInfo> getRoleInfoList() throws JsonProcessingException {
        List<RoleInfo> roleInfoList = null;
        // 1.从redis拿
        String json = redisService.get("roleInfoList");

        // 2.没有则从数据库查，并放入redis
        if (json != null)
            roleInfoList = MyJsonUtil.toBeanArray(json, RoleInfo.class);
        else {
            roleInfoList = roleMapper.getRoleInfoList();
            redisService.setEx("roleInfoList", MyJsonUtil.writeToString(roleInfoList));
        }
        return roleInfoList;
    }

    @Override
    @Transactional//开启事务
    public RoleInfo insertRole(Role role, int loginId) {
        // 1.完善插入数据
        role.setCreateBy(loginId);
        role.setEditBy(loginId);
        role.setFlag(0);

        // 2.检查是否有同名role
        List<Role> roles = roleMapper.selectByName(role.getName());
        if (roles.size() != 0)
            throw new MyException(400, "角色名：" + role.getName() + " 已存在");

        // 3.插入
        int i = roleMapper.insert(role);
        if (i != 1)
            throw new MyException(500, "插入失败");

        // 4.查询info
        RoleInfo roleInfo = roleMapper.getRoleInfoById(role.getId());

        // 5.redis删除roleInfoList
        redisService.delete("roleInfoList");
        return roleInfo;
    }

    @Override
    @Transactional//开启事务
    public RoleInfo updateRole(Role role, int loginId) throws JsonProcessingException {
        // 1.完善插入数据
        role.setEditBy(loginId);
        if ("root".equals(role.getName()))
            throw new MyException("不能修改为root");
        List<Role> roles = roleMapper.selectByName(role.getName());
        if (roles.size() != 0 && !roles.get(0).getId().equals(role.getId()))
            throw new MyException(400, "已经存在该角色名：" + role.getName());

        // 2.更新
        int i = roleMapper.updateById(role);
        if (i != 1)
            throw new MyException(500, "更新失败，可能是id不存在：" + role.getId());

        // 3.查询info
        RoleInfo roleInfo = roleMapper.getRoleInfoById(role.getId());

        // 4.放入redis
        redisService.setEx("roleInfo:id:" + role.getId(), MyJsonUtil.writeToString(roleInfo));

        // 5.清redis的roleInfoList
        redisService.delete("roleInfoList");
        return roleInfo;
    }

    @Override
    public RoleInfo deleteRole(int roleId, int loginId) {
        // 1.查询出roleInfo
        RoleInfo roleInfo = roleMapper.getRoleInfoById(roleId);
        if (roleInfo == null)
            throw new MyException(400, "角色id不存在：" + roleId);
        if ("root".equals(roleInfo.getName()))
            throw new MyException(400, "不能删除root角色");
        // 2.删除操作
        int i = roleMapper.logicalDelete(roleId, loginId);
        if (i != 1)
            throw new MyException(500, "删除失败");
        // 3.删除redis的roleInfo和roleInfoList
        redisService.delete("roleInfoList", "roleInfo:id:" + roleId);
        return roleInfo;
    }
}
